﻿using System;
using System.Runtime.InteropServices;



namespace Assist
{
    public static class ModelessFormUtil
    {
        [DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        [DllImport("user32")]
        internal static extern bool EnumChildWindows(IntPtr window, EnumWindowProc callback, IntPtr lparam);
        internal delegate bool EnumWindowProc(IntPtr hWnd, IntPtr parameter);//回调函数
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        public static IntPtr mdi;

        public static IntPtr MDI()
        {
            FindChildClassHwnd(Autodesk.Windows.ComponentManager.ApplicationWindow, IntPtr.Zero);
            if (mdi != IntPtr.Zero)
            {
                return mdi;
            }
            return IntPtr.Zero;
        }
        internal static bool FindChildClassHwnd(IntPtr hwndParent, IntPtr lParam)
        {
            EnumWindowProc childProc = new EnumWindowProc(FindChildClassHwnd);
            IntPtr hwnd = FindWindowEx(hwndParent, IntPtr.Zero, "MDIClient", null);
            if (hwnd != IntPtr.Zero)
            {
                mdi = hwnd;
                return false;
            }
            EnumChildWindows(hwndParent, childProc, IntPtr.Zero);
            return true;
        }

        [DllImport("user32.dll")]
        public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        [DllImport("user32.dll")]
        public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

    }
}
